package exception;

public class TryCatchDemo {
    public static void main(String[] args) {
        System.out.println("程序开始了");

        try {
                String line = "null";
            System.out.println(line.length());
            System.out.println(line.charAt(4));
            System.out.println(Integer.parseInt(line));//将字符串转换成整型，前提是输入的字符串是数字
            System.out.println(line.length());//前面代码但凡报错，后面代码都不会执行
        } catch (NullPointerException e) {
            System.out.println("捕获到空指针异常: " + e.getMessage());
        } catch (StringIndexOutOfBoundsException e){
            System.out.println("捕获到字符串索引越界异常: " + e.getMessage());
        } catch (Exception | Error e){
            System.out.println("捕获到异常: " + e.getMessage());
        }
        System.out.println("程序结束了");
    }
}
